<template>
	<view class="mainbgc">
		<header-top :messageInfo="{typeFlag:true,message:'修改单据明细',liner:false}">
			<view class="rightmsg" slot="rightmsg" v-if="dataAll.goods_type!=3">
				<view class="rightmsg_content flexleft">
					<view class="shanchuer" @click="delet"></view>
				</view>
			</view>
		</header-top>
		<u-datetime-picker :show="show" v-model="value1" mode="date" @confirm="confirm" @cancel="show=false">
		</u-datetime-picker>
		<view class="mainpadding ffffff margin_top">
			<view class="inputbox margin_top">
				<view class="flexleft flex1">
					<view class="strongtext margin_right shangbianju">商品名称</view>
				</view>
				<view class="itembox flexbetween">
					<input type="text" class="inputs" placeholder="请输入商品名称" disabled="" v-model="dataAll.goodsinfo.name">
				</view>
			</view>
			<view class="inputbox margin_top" v-if="dataAll.goodsinfo.producedate_status==1">
				<view class="flexleft flex1">
					<view class="strongtext margin_right shangbianju">生产日期</view>
				</view>
				<!-- 采购单生产日期（自定义选择） -->
				<view class="itembox flexbetween" v-if="type==3 || type==2"  @click="getProductTime()">
					<input type="text" class="inputs" placeholder="选择生产日期" disabled="true" v-model="dataAll.produce_time">
					<view class="righticon2"></view>
				</view>
				<view class="itembox flexbetween" v-else @click="show=true">
					<input type="text" class="inputs" placeholder="选择生产日期" disabled="true" v-model="dataAll.produce_time">
					<view class="righticon2"></view>
				</view>
			</view>
			<view class="inputbox margin_top"  v-if="dataAll.goodsinfo.producedate_status==1">
				<view class="flexleft flex1">
					<view class="strongtext margin_right shangbianju">保质期</view>
				</view>
				<view class="itembox">
					<input type="number" class="inputs" placeholder="请填写保质期" disabled="" v-model="dataAll.goodsinfo.bzq">
				</view>
			</view>
			<view class="inputbox margin_top">
				<view class="flexleft flex1">
					<view class="strongtext margin_right shangbianju">单位</view>
				</view>
				<view class="itembox flexbetween">
					<input type="text" class="inputs" placeholder="点击选择单位" disabled="true" v-model="dataAll.goods_sku_name">
				</view>
			</view>
			<view class="inputbox margin_top"  v-if="type!=3 && type!=5  && type!=6 && type!=7">
				<view class="flexleft flex1">
					<view class="strongtext margin_right shangbianju">单价</view>
				</view>
				<view class="itembox">
					<input type="number" class="inputs" placeholder="请输入单价" v-model="dataAll.price" >
				</view>
			</view>
			<!-- 销售单赠品和促销商品价格不可输入 -->
			<view class="inputbox margin_top"  v-if="type==3">
				<view class="flexleft flex1">
					<view class="strongtext margin_right shangbianju">单价</view>
				</view>
				<view class="itembox">
					<input type="number" class="inputs" placeholder="请输入单价" :disabled="dataAll.goods_type!=1" v-model="dataAll.price" >
				</view>
			</view>
			<view class="inputbox margin_top"  v-if="type!=3">
				<view class="flexleft flex1">
					<view class="strongtext margin_right shangbianju">数量</view>
				</view>
				<view class="itembox">
					<input type="number" class="inputs" placeholder="请输入数量" v-model="dataAll.num">
				</view>
			</view>
			<view class="inputbox margin_top"  v-if="type==3">
				<view class="flexleft flex1">
					<view class="strongtext margin_right shangbianju">数量</view>
				</view>
				<view class="itembox">
					<input type="number" class="inputs" placeholder="请输入数量" :disabled="dataAll.goods_type!=1" v-model="dataAll.num">
				</view>
			</view>
			<view class="inputbox margin_top"  v-if="type!=3 && type!=5 && type!=6 && type!=7">
				<view class="flexleft flex1">
					<view class="strongtext margin_right shangbianju">金额</view>
				</view>
				<view class="itembox">
					<input type="number" class="inputs" placeholder="请输入金额" disabled="" :value="dataAll.num*dataAll.price">
				</view>
			</view>
			<view class="inputbox margin_top"  v-if="type==3">
				<view class="flexleft flex1">
					<view class="strongtext margin_right shangbianju">金额</view>
				</view>
				<view class="itembox">
					<input type="number" class="inputs" placeholder="请输入金额" :disabled="dataAll.goods_type!=1 || dataAll.goods_type!=3" :value="dataAll.num*dataAll.price">
				</view>
			</view>
			<view class="inputbox margin_top" v-if="dataAll.goods_type!=3">
				<view class="flexleft flex1">
					<view class="strongtext margin_right shangbianju">备注</view>
				</view>
				<view class="itembox">
					<input type="text" class="inputs" placeholder="请输入备注" v-model="dataAll.notes">
				</view>
			</view>
			<view class="inputbox margin_top"  v-if="(type==3 || type==4) && dataAll.goods_type!=3">
				<view class="flexleft flex1">
					<view class="strongtext margin_right shangbianju">赠品数量</view>
				</view>
				<view class="itembox">
					<input type="number" class="inputs" placeholder="请输入数量" v-model="dataAll.send_num">
				</view>
			</view>
			<view class="inputbox margin_top"  v-if="(type==3 || type==4) && dataAll.goods_type!=3">
				<view class="flexleft flex1">
					<view class="strongtext margin_right shangbianju">赠送备注</view>
				</view>
				<view class="itembox">
					<input type="text" class="inputs" placeholder="请输入赠送备注" v-model="dataAll.send_notes">
				</view>
			</view>
		</view>
		<view class="yesbtn" @click="submit">确认</view>
		<u-picker :show="timePickershow" :columns="timePickercolumns" @confirm="timePickersure" title="选择生产日期"
			keyName="produce_time_text" @cancel="timePickershow=false"></u-picker>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				// 日期选择
				show: false,
				value1: Number(new Date()),
				timePickershow: false,
				timePickercolumns: [],
				type:1,//1采购单,2采购退货单,3销售单
				warehouse_id:"",
				dataAll:{},
				index:"",//当前编辑数据的下标
			}
		},
		// 采购单生产日期（自定义选择）
		onLoad(options) {
			let data = JSON.parse(decodeURIComponent(options.msg));
			this.type = data.type
			this.dataAll = data.item
			this.index = data.index
			if(this.type==3){
				this.warehouse_id = data.warehouse_id
			}
		},
		methods: {
			submit() {
				let _this = this;
				if(this.type==1){//采购单
					let caigoudanInfo = JSON.parse(uni.getStorageSync("caigoudanInfo"))
					caigoudanInfo.data.splice(this.index,1,this.dataAll)
					uni.setStorageSync('caigoudanInfo', JSON.stringify(caigoudanInfo))
					httpRequest.toast("修改成功");
					setTimeout(function(){
						uni.navigateBack({
							delta: 1
						})
					},500)
				}
				if(this.type==2){//采购退货单
					let caigoutuihuoInfo = JSON.parse(uni.getStorageSync("caigoutuihuoInfo"))
					
					caigoutuihuoInfo.data.splice(this.index,1,this.dataAll)
					uni.setStorageSync('caigoutuihuoInfo', JSON.stringify(caigoutuihuoInfo))
					httpRequest.toast("修改成功");
					setTimeout(function(){
						uni.navigateBack({
							delta: 1
						})
					},500)
				}
				if(this.type==3){//销售单
					let xiaoshoudanInfo = JSON.parse(uni.getStorageSync("xiaoshoudanInfo"))
					xiaoshoudanInfo.data.splice(this.index,1,this.dataAll)
					uni.setStorageSync('xiaoshoudanInfo', JSON.stringify(xiaoshoudanInfo))
					httpRequest.toast("修改成功");
					setTimeout(function(){
						uni.navigateBack({
							delta: 1
						})
					},500)
				}
				if(this.type==4){//退货单
					let tuihuodanInfo = JSON.parse(uni.getStorageSync("tuihuodanInfo"))
					tuihuodanInfo.data.splice(this.index,1,this.dataAll)
					uni.setStorageSync('tuihuodanInfo', JSON.stringify(tuihuodanInfo))
					httpRequest.toast("修改成功");
					setTimeout(function(){
						uni.navigateBack({
							delta: 1
						})
					},500)
				}
				if(this.type==5){//入库单
					let rukudanInfo = JSON.parse(uni.getStorageSync("rukudanInfo"))
					rukudanInfo.data.splice(this.index,1,this.dataAll)
					uni.setStorageSync('rukudanInfo', JSON.stringify(rukudanInfo))
					httpRequest.toast("修改成功");
					setTimeout(function(){
						uni.navigateBack({
							delta: 1
						})
					},500)
				}
				if(this.type==6){//出库单
					let chukudanInfo = JSON.parse(uni.getStorageSync("chukudanInfo"))
					chukudanInfo.data.splice(this.index,1,this.dataAll)
					uni.setStorageSync('chukudanInfo', JSON.stringify(chukudanInfo))
					httpRequest.toast("修改成功");
					setTimeout(function(){
						uni.navigateBack({
							delta: 1
						})
					},500)
				}
				if(this.type==7){//调拨单
					let diaobodanInfo = JSON.parse(uni.getStorageSync("diaobodanInfo"))
					diaobodanInfo.data.splice(this.index,1,this.dataAll)
					uni.setStorageSync('diaobodanInfo', JSON.stringify(diaobodanInfo))
					httpRequest.toast("修改成功");
					setTimeout(function(){
						uni.navigateBack({
							delta: 1
						})
					},500)
				}
			},
			// 删除当前商品
			delet(){
				if(this.type==1){//采购单
					let caigoudanInfo = JSON.parse(uni.getStorageSync("caigoudanInfo"))
					caigoudanInfo.data.splice(this.index,1)
					uni.setStorageSync('caigoudanInfo', JSON.stringify(caigoudanInfo))
					httpRequest.toast("删除成功");
					setTimeout(function(){
						uni.navigateBack({
							delta: 1
						})
					},500)
				}
				if(this.type==2){//采购退货单
					let caigoutuihuoInfo = JSON.parse(uni.getStorageSync("caigoutuihuoInfo"))
					caigoutuihuoInfo.data.splice(this.index,1)
					uni.setStorageSync('caigoutuihuoInfo', JSON.stringify(caigoutuihuoInfo))
					httpRequest.toast("删除成功");
					setTimeout(function(){
						uni.navigateBack({
							delta: 1
						})
					},500)
				}
				if(this.type==3){//退货单
					let xiaoshoudanInfo = JSON.parse(uni.getStorageSync("xiaoshoudanInfo"))
					xiaoshoudanInfo.data.splice(this.index,1)
					uni.setStorageSync('xiaoshoudanInfo', JSON.stringify(xiaoshoudanInfo))
					httpRequest.toast("删除成功");
					setTimeout(function(){
						uni.navigateBack({
							delta: 1
						})
					},500)
				}
				if(this.type==4){//销售单
					let tuihuodanInfo = JSON.parse(uni.getStorageSync("tuihuodanInfo"))
					tuihuodanInfo.data.splice(this.index,1)
					uni.setStorageSync('tuihuodanInfo', JSON.stringify(tuihuodanInfo))
					httpRequest.toast("删除成功");
					setTimeout(function(){
						uni.navigateBack({
							delta: 1
						})
					},500)
				}
				if(this.type==5){//入库单
					let rukudanInfo = JSON.parse(uni.getStorageSync("rukudanInfo"))
					rukudanInfo.data.splice(this.index,1)
					uni.setStorageSync('rukudanInfo', JSON.stringify(rukudanInfo))
					httpRequest.toast("删除成功");
					setTimeout(function(){
						uni.navigateBack({
							delta: 1
						})
					},500)
				}
				if(this.type==6){//出库单
					let chukudanInfo = JSON.parse(uni.getStorageSync("chukudanInfo"))
					chukudanInfo.data.splice(this.index,1)
					uni.setStorageSync('chukudanInfo', JSON.stringify(chukudanInfo))
					httpRequest.toast("删除成功");
					setTimeout(function(){
						uni.navigateBack({
							delta: 1
						})
					},500)
				}
				if(this.type==7){//调拨单
					let diaobodanInfo = JSON.parse(uni.getStorageSync("diaobodanInfo"))
					diaobodanInfo.data.splice(this.index,1)
					uni.setStorageSync('diaobodanInfo', JSON.stringify(diaobodanInfo))
					httpRequest.toast("删除成功");
					setTimeout(function(){
						uni.navigateBack({
							delta: 1
						})
					},500)
				}
			},
			// 获取生产日期
			getProductTime() {
				uni.showLoading({
					mask: true,
					title: "请稍候。。。"
				})
				let dataAll = this.dataAll
				let difference_type = dataAll.goods_sku_id==dataAll.goodsinfo.big_goods_unit_id?1:dataAll.goods_sku_id==dataAll.goodsinfo.middle_goods_unit_id?2:3
				httpRequest.request('/dhtapi/Saleorder/getGoodsProduceTime', 'GET', {
					goods_id: this.dataAll.goodsinfo.id,
					warehouse_id: this.warehouse_id,
					difference_type
				}, false, false, true).then(res => {
					uni.hideLoading()
					this.timePickercolumns = []
					if (res.data.length) {
						this.timePickercolumns.push(res.data.reverse())
						this.timePickershow = true
					} else {
						httpRequest.toast("当前仓库无此类商品！")
					}
				}).catch(err => {})
			},
			timePickersure(e){
				this.dataAll.produce_time = e.value[0].produce_time_text
				this.timePickershow = false
			},
			// 选择生产日期
			confirm(e) {
				this.dataAll.produce_time = httpRequest.returnDate(e.value);
				this.show = false;
			},
		}
	}
</script>
<style lang="scss" scoped>
	.shanchuer {
		width: 36rpx;
		height: 36rpx;
		background-image: url('@/static/image/system/shanchu2.png');
		background-size: 100% 100%;
	}
</style>
<style lang="scss" scoped>
	.inputs {
		font-size: 28rpx;
	}

	.huisetext {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}

	//
	.flex1 {
		flex: 3;
	}

	.inputbox {
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
	}

	.itembox {
		flex: 7;
		width: 100%;
		padding-bottom: 20rpx;
		border-bottom: 1rpx solid rgba(153, 153, 153, 0.06);
	}

	.lanseyoujt {
		background-image: url('@/static/image/system/lanseyoutubiao.png');
		width: 12rpx;
		height: 22rpx;
		margin: 0 30rpx 0 10rpx;
		background-size: 100% 100%;
	}

	.lansetext {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #0BBDCB;
	}

	.shangbianju {
		margin-top: 10rpx;
	}
</style>
